//
//  YWSettingViewController.m
//  百布业务
//
//  Created by 陈炳桦 on 15/10/26.
//  Copyright © 2015年 陈炳桦. All rights reserved.
//

#import "YWSettingViewController.h"
#import "YWUserInfo.h"

@interface YWSettingViewController ()

// 当前选中按钮
@property (nonatomic,assign) NSInteger currentIndex;

@end

@implementation YWSettingViewController

- (instancetype)init
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"应用设置";
    
    //默认选中
    self.currentIndex = [[YWUserInfo sharedUseInfo].photoQuality intValue];

}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [[UITableViewCell alloc] init];
    
    if (indexPath.row == 0){
        cell.textLabel.text = @"高等品质";
    }else if(indexPath.row == 1){
        cell.textLabel.text = @"中等品质";
    }else if(indexPath.row == 2){
        cell.textLabel.text = @"低等品质";
    }
    
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    // 默认选中第二个
    YWUserInfo *userInfo = [YWUserInfo sharedUseInfo];
    if (indexPath.row == [userInfo.photoQuality intValue]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    return @"首页求购图片质量";
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 设置为单选状态
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if(indexPath.row == _currentIndex){
        return;
    }
    NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:_currentIndex
                                                   inSection:0];
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    if (newCell.accessoryType == UITableViewCellAccessoryNone) {
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
    if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
        oldCell.accessoryType = UITableViewCellAccessoryNone;
    }
    _currentIndex=indexPath.row;
    
    // 保存设置
    YWUserInfo *userInfo = [YWUserInfo sharedUseInfo];
    userInfo.photoQuality = @(_currentIndex);
}

@end
